<?php
/**
 *  玩家领取邮件附件
 */

class ReceiveEmail extends APIBase {
	public $tag = "ReceiveEmail";
	public $isLogin = true;
	
	public function before() {
		$this->initMysql();
		$this->inithttp();
		$this->initCacheRedis();
		return true;
	}
			
	public function logic() {
		$eid = $this->param['eid'];//邮件id
	    $uid = $this->uid;
		$emailinfo = $this->cache_redis->hGetAll('hemailbyuser:'.$uid.':'.$eid);
		$data = array();
		if(!empty($emailinfo)){
			//改变邮件领取状态
			if($emailinfo['is_receive']==0 && $emailinfo['is_read']!=9){
			    $hmg = $this->cache_redis->hMget('hmg:'.$emailinfo['mid'],array('rid'));
			    //获取奖励id
			    $ridArr = explode(',',$hmg['rid']);
			    $reward = array();
			    for($i=0;$i<count($ridArr);$i++){
				    $hreward = $this->cache_redis->hMget('hreward:'.$ridArr[$i],array('propid','reward_quantity'));
				    $keys = $this->cache_redis->keys('hbackpack:'.$uid.':*');
				    if(count($keys)<=0){//背包没道具
					    $data = array('uid'=>$uid,'propid'=>(int)$hreward['propid'],'count'=>$hreward['reward_quantity'],'createtime'=>time(),'status'=>1);
					    $this->cache_redis->hMset('hbackpack:'.$uid.':'.(int)$hreward['propid'],$data);
				    }else{//背包有道具
				    //判断是否存在相同道具
					    $isExistsProp = $this->cache_redis->exists('hbackpack:'.$uid.':'.(int)$hreward['propid']);
					    if($isExistsProp){
						    $row = $this->cache_redis->hMget('hbackpack:'.$uid.':'.(int)$hreward['propid'],array('count'));
						    if($hreward['propid']==1){//房卡
						        $count = 0;
							    $count = (int)$row['count']+(int)$hreward['reward_quantity'];
							    $this->cache_redis->hMset('hbackpack:'.$uid.':'.(int)$hreward['propid'],array('count'=>$count,'status'=>1));
							    $user = $this->data_redis->hMGet('hu:'.$uid,array('round_card_num'));
							    $round_card_num = (int)$user['round_card_num']+(int)$hreward['reward_quantity'];
					            $this->data_redis->hMset('hu:'.$uid,array('round_card_num'=>$round_card_num));
						    }else{//其他道具
							
						    }
					    }else{
					    //不存在相同道具,则添加记录
					        $data = array('uid'=>$uid,'propid'=>(int)$hreward['propid'],'count'=>$hreward['reward_quantity'],'createtime'=>time(),'status'=>1);
					        $this->cache_redis->hMset('hbackpack:'.$uid.':'.(int)$hreward['propid'],$data);
					    }
				    }
			    }
				$this->cache_redis->hMset('hemailbyuser:'.$uid.':'.$eid,array('is_read'=>1,'is_receive'=>1,'update_date'=>time()));
				$this->returnData('success');
		    }else{
				$this->returnError(302,'已领取奖励！！');
			}
		}else{
			$this->returnError(301,'无效参数！！');
		}
	}

	public function after() {
		$this->deinitDataRedis();
		$this->deinitMysql();
	}
}

?>
